<template>
    <el-dialog :title="title" :visible="dialogVisible" @close="cancelClick">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="权限名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入权限名称" style="width: 80%;"></el-input>
            </el-form-item>
            <el-form-item label="权限标识" prop="code">
                <el-input v-model="form.code" placeholder="请输入权限标识" style="width: 80%;"></el-input>
            </el-form-item>
            <el-form-item label="权限描述" prop="description">
                <el-input v-model="form.description" placeholder="请输入权限描述" style="width: 80%;"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="cancelClick">取 消</el-button>
            <el-button type="primary" @click="confirmClick">确 定</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { addPermission, updPermission } from '@/api/permission'

export default {
    name: 'AddPermission',
    props: {
        // 弹层的标题
        title: {
            type: String,
            required: true
        },
        // 弹层的展示
        dialogVisible: {
            type: Boolean,
            required: true
        },
        // 权限类型
        type: {
            type: Number,
            required: true
        },
        // 权限id
        id: {
            type: String,
            required: true
        }
    },
    data() {
        return {
            form: {
                name: '',
                code: '',
                description: ''
            },
            rules: {
                name: [
                    {required: true, message: '请输入权限名称', trigger: 'blur'}
                ],
                code: [
                    {required: true, message: '请输入权限标识', trigger: 'blur'}
                ],
                description: [
                    {required: true, message: '请输入权限描述', trigger: 'blur'}
                ]
            }
        }
    },
    methods: {
        // 确定提交（新增或编辑操作）
        confirmClick() {
            this.$refs['form'].validate(async (valid) => {
                if(valid) {
                    // 校验通过
                    if(this.title === '新增权限') {
                        // 新增操作
                        await addPermission({...this.form, pid: this.id, type: this.type})
                        this.$message.success('新增权限成功')
                    } else {
                        // 编辑操作
                        await updPermission(this.form)
                        this.$message.success('编辑权限成功')
                    }
                    this.$emit('update:dialogVisible', false)
                    this.$parent.getPermissionList()
                } else {
                    // 校验失败
                    console.log('校验未通过');
                    return false;
                }
            })
        },
        // 取消操作
        cancelClick() {
            this.form = {
                name: '',
                code: '',
                description: ''
            }
            this.$refs['form'].resetFields()
            // 关闭弹层
            this.$emit('update:dialogVisible', false)
        }
    }
}
</script>

<style></style>